using System.Drawing;
using System.Collections;
using System.Collections.Generic;

namespace Nate.Pathfinding
{
    public class Graph
    {
        public Graph(List<Node> nodes)
        {
            _nodes = nodes;

            // Generate the edge list. The node with the lesser index spawns.
            _edges = new List<Edge>();
            foreach (Node n in _nodes)
            {
                foreach (NUtils.SortablePair<double, Node> pair in n.Neighbors)
                {
                    double cost = pair.SortBy;
                    Node neighbor = pair.Value;

                    if (n.Index < neighbor.Index)
                    {
                        Edge e = new Edge(_edges.Count, n.Index, neighbor.Index, cost);
                        _edges.Add(e);
                        n.Edges.Add(e);
                        neighbor.Edges.Add(e);
                    }
                }
            }
        }

        public NodeID FindNodeID(Point point)
        {
            for (int i = 0; i < _nodes.Count; ++i)
            {
                if (_nodes[i].Point == point)
                {
                    return new NodeID(i);
                }
            }

            throw new Graph.Exception.NotInGraph(string.Format("Failed to find point ({0},{1}) in this graph.", point.X, point.Y));
        }
        
        public List<Pathfinding.Node> Nodes
        {
            get
            {
                return _nodes;
            }
        }
        public List<Pathfinding.Edge> Edges
        {
            get
            {
                return _edges;
            }
        }

        private List<Pathfinding.Node> _nodes;
        private List<Pathfinding.Edge> _edges;

        #region Exceptions


        public class Exception
        {
            public class NotInGraph : System.Exception { public NotInGraph(string s):base(s){} }
        }

        
        #endregion
    }
}